<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
    <!--text是指对象为网页中的文本,css或是javascript是指当前指定的文本类型-->
    <style type="text/css"> 
        span {
            padding: 3px;
            margin: 3px;
        }
    
        .demo {
            width: 500px;
            height: 200px;
            margin: 150px auto;
            border: 1px solid #ccc;
        }
    
        .current {
            background: #ccc;
        }
    
        .red {
            background: red;
        }
    </style>
</head>



<body>
    <div id="app">
        <div class="demo">
            <p>当前的选择是
                <span class="current"
                 v-for='item,key in choose'
                >
                 {{item}}
                    <i @click="removeHandle(key)">x</i>
                </span>
            </p>
            <ul>
                <li v-for='item,index in datalist'>
                    {{item.title}}
                    <span
                        v-for="option,i in item.list"
                        @click="addChooseHandle(option,index,i)"
                        :class='{red:item.index === i}'
                    >
                    {{option}}
                    </span>
                </li>
            </ul>
        </div>
    </div>
</body>

</html>
<script type="text/javascript"> //表明放在之间的是文本类型(text),javascript是告诉浏览器里面的text是个javascript脚本
    let data = [
        {
            title: '品牌',
            list: ['小米', '苹果', '魅族', '三星', '华为']
        },
        {
            title: '尺寸',
            list: ['5.1', '5.5', '5.7', '5.9', '5.2']
        },
        {
            title: '系统',
            list: ['windows', 'ios', 'andon', 'linux', 'xp']
        }
    ];

    //使用foreach添加data

    data.forEach(item => item.index = -1);
    //查看data中的数据
    console.log(data);

    new Vue({
        el: "#app",
        data: {
            datalist: data,
            choose: {

            }
        },
        methods: {
            addChooseHandle(option, index, i) {
                //使用set添加对象
                this.$set(this.choose, index, option);
                console.log(this.choose);
                //找到操作的一行，把这一行的数据中的index，设置为
                this.datalist[index].index = i;
            },
            removeHandle(key){
                console.log(key);
                this.$delete(this.choose,key);
                this.datalist[key].index=-1;
            }
        }

    })

</script>